package _17_剑指Offer;

public class _15_剑指Offer二进制中1的个数 {

    public static void main(String[] args) {

        _15_剑指Offer二进制中1的个数 v = new _15_剑指Offer二进制中1的个数();

        System.out.println(v.hammingWeight(-3));

    }

    // n & (n - 1)是将n的最低位变为0
    public int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            n &= (n - 1);
            count++;
        }
        return count;
    }

    // 移位，统计1的数量
    public int hammingWeight1(int n) {
        int count = 0;
        int num = 0;
        while (num++ < 32) {
            if ((n & 1) == 1) {
                count++;
            }
            n >>= 1;
        }
        return count;
    }

}
